﻿Imports System
Imports System.IO
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports Microsoft.VisualBasic
Imports Lucene.Net.Search
Imports System.Data

Partial Public Class LogViewer
    Private lngTemp, PathVar, Time, Message, User As String
    Dim Results As System.Collections.Generic.List(Of Search.Result)
#Region " Buttons"
    Private Sub Close_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles CloseButton.MouseDown
        Dim ISC As New ImageSourceConverter
        CloseButton.Source = ISC.ConvertFrom("pack://application:,,,/Images/Close-pressed.png")
    End Sub
    Private Sub Close_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles CloseButton.MouseEnter
        Dim ISC As New ImageSourceConverter
        CloseButton.Source = ISC.ConvertFrom("pack://application:,,,/Images/close-hover.png")
    End Sub
    Private Sub Close_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles CloseButton.MouseLeave
        Dim ISC As New ImageSourceConverter
        CloseButton.Source = ISC.ConvertFrom("pack://application:,,,/Images/close.png")
    End Sub
    Private Sub Close_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles CloseButton.MouseUp
        Dim ISC As New ImageSourceConverter
        CloseButton.Source = ISC.ConvertFrom("pack://application:,,,/Images/close.png")
        GC.Collect()
        Me.Close()
    End Sub
    Private Sub Max_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Max.MouseDown
        Dim ISC As New ImageSourceConverter
        Max.Source = ISC.ConvertFrom("pack://application:,,,/Images/Maximize-pressed.png")
    End Sub
    Private Sub Max_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Max.MouseEnter
        Dim ISC As New ImageSourceConverter
        Max.Source = ISC.ConvertFrom("pack://application:,,,/Images/Maximize-hover.png")
    End Sub
    Private Sub Max_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Max.MouseLeave
        Dim ISC As New ImageSourceConverter
        Max.Source = ISC.ConvertFrom("pack://application:,,,/Images/Maximize.png")
    End Sub
    Private Sub Max_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Max.MouseUp
        Dim ISC As New ImageSourceConverter
        Min.Source = ISC.ConvertFrom("pack://application:,,,/Images/Minimize.png")
        If Me.WindowState <> Windows.WindowState.Maximized Then
            Me.WindowState = Windows.WindowState.Maximized
        Else
            Me.WindowState = Windows.WindowState.Normal
        End If
    End Sub
    Private Sub Min_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Min.MouseDown
        Dim ISC As New ImageSourceConverter
        Min.Source = ISC.ConvertFrom("pack://application:,,,/Images/Minimize-pressed.png")
    End Sub
    Private Sub Min_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Min.MouseEnter
        Dim ISC As New ImageSourceConverter
        Min.Source = ISC.ConvertFrom("pack://application:,,,/Images/Minimize-hover.png")
    End Sub
    Private Sub Min_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Min.MouseLeave
        Dim ISC As New ImageSourceConverter
        Min.Source = ISC.ConvertFrom("pack://application:,,,/Images/Minimize.png")
    End Sub
    Private Sub Min_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Min.MouseUp
        Dim ISC As New ImageSourceConverter
        Min.Source = ISC.ConvertFrom("pack://application:,,,/Images/Minimize.png")
        Me.WindowState = Windows.WindowState.Minimized
    End Sub
#End Region
    Public Sub New()
        MyBase.New()
        Me.InitializeComponent()
        PathVar = CoreFX.API.GetDataPath() & "Logs"
        LoadAvailLogs()
    End Sub
    Private Sub LoadAvailLogs()
        Dim Node, Node2, Node3 As TreeViewItem
        Node = New TreeViewItem
        Node.Header = "MChat Logs"
        Me.AvailLogs.Items.Add(Node)
        Dim Dirs As String() = System.IO.Directory.GetDirectories(PathVar)
        For Each Dir As String In Dirs
            If Dir <> "" Then
                Node2 = New TreeViewItem
                Node2.Header = GetChannelName(Dir)
                Node.Items.Add(Node2)
                For Each Log As String In Directory.GetFiles(Dir, "*.txt")
                    If Log <> "" Then
                        Node3 = New TreeViewItem
                        Node3.Header = RemoveIndexedFromPath(Path.GetFileNameWithoutExtension(Log))
                        Node3.Tag = Log
                        Node2.Items.Add(Node3)
                        Node2.IsExpanded = True
                    End If
                Next
            End If
        Next
        Node.IsExpanded = True
    End Sub
    Private Function RemoveIndexedFromPath(ByVal Path As String) As String
        Dim A As String
        A = Path.Replace("-indexed", " ")
        Return Trim(A)
    End Function
    Private Function GetChannelName(ByVal Path As String) As String
        Dim I As Integer
        Dim A, B As String
        I = Path.LastIndexOf("\")
        A = Mid(Path, 1, I + 1)
        B = Path.Replace(A, " ")
        Return Trim(B)
    End Function
    Private Sub AvailLogs_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles AvailLogs.SelectedItemChanged
        Dim SelNode As TreeViewItem = AvailLogs.SelectedItem
        If Not SelNode.Tag Is Nothing Then
            LogReader.Text = Nothing
            GC.Collect()
            FileOpen(1, SelNode.Tag, OpenMode.Input)
            Do Until EOF(1) = True
                Microsoft.VisualBasic.Input(1, lngTemp)
                LogReader.AppendText(lngTemp & vbNewLine)
            Loop
            FileClose()
        End If
    End Sub
    Private Sub ExecuteQuery()
        Dim Searcher As Search.IndexSearcherMC = New Search.IndexSearcherMC(CoreFX.API.GetDataPath() & "Logs Index")
        Dim sQuery As String = SearchTerm.Text
        Results = Searcher.SearchIndex(sQuery)
        SearchResults1.DataContext = CreateDataTable()
    End Sub
    Private Sub SearchTerm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles SearchTerm.KeyDown
        If e.Key = Input.Key.Enter Then
            ExecuteQuery()
        End If
    End Sub
    Private Function CreateDataTable() As DataTable
        Dim tbl As DataTable = New DataTable("SearchResults")
        Dim I As Integer
        tbl.Columns.Add("User")
        tbl.Columns.Add("Date")
        tbl.Columns.Add("Time")
        tbl.Columns.Add("Channel")
        tbl.Columns.Add("Message")
        For Each Item As Search.Result In Results
            I = I + 1
            tbl.Rows.Add(I, Item.User, Item.Date1, Item.Time, "None", Item.Message)
        Next
        Return tbl
    End Function
End Class
Namespace Search
    Public Structure Result
        Dim User As String
        Dim Date1 As String
        Dim Time As String
        Dim Message As String
        Dim Channel As String
    End Structure
    Public Class IndexSearcherMC
        Inherits Lucene.Net.Search.IndexSearcher
        Public Sub New(ByVal IndexPath As String)
            MyBase.New(IndexPath)
        End Sub
        Public Function SearchIndex(ByVal querytext As String) As System.Collections.Generic.List(Of Result)
            Dim IX As Integer = 0
            Dim Query As Lucene.Net.Search.Query
            Dim SR As Result = New Result
            Query = Lucene.Net.QueryParsers.QueryParser.Parse(querytext, "User", New Lucene.Net.Analysis.Standard.StandardAnalyzer())
            Dim Results As New System.Collections.Generic.List(Of Result)
            Dim Hits As Lucene.Net.Search.Hits = MyBase.Search(Query)
            For IX = 0 To Hits.Length - 1
                Dim doc As Lucene.Net.Documents.Document
                doc = Hits.Doc(IX)
                SR.User = doc.Get("User")
                SR.Date1 = doc.Get("Date")
                SR.Message = doc.Get("Message")
                SR.Time = doc.Get("Time")
                Results.Add(SR)
            Next
            Return Results
        End Function
    End Class
    Public Class IndexQueue
        Inherits System.Collections.Generic.List(Of CoreFX.LogSearch.LTIItem)
        Public Sub AddItemToQueue(ByVal Item As CoreFX.LogSearch.LTIItem)
            MyBase.Add(Item)
        End Sub
    End Class
End Namespace